package com.ndd.service.impl;

import com.ndd.dto.CartDTO;
import com.ndd.dto.CartNumUpdateDTO;
import com.ndd.entity.Cart;
import com.ndd.entity.Product;
import com.ndd.enums.ServiceCode;
import com.ndd.exception.ServiceException;
import com.ndd.mapper.CartMapper;
import com.ndd.service.CartService;
import com.ndd.vo.CartVO;
import com.ndd.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
@Slf4j
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public void addCart(@RequestBody @Validated CartDTO cartDTO, UserLoginVO userLoginVO) {
        /**
         * 根据商品id和上架状态去查询商品？
         * id对应正确，不正确商品不存在
         * 上架状态status为上架（1）通过
         * 否则抛出异常
         */
        Product product = cartMapper.getProductId(cartDTO.getProductId());
        if (product == null || product.getStatus() != 1) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "商品不存在或已经下架");
        }
        log.debug("获取的购物车:{}", product);
        //通过，商品存在
        //调用用户id和商品id,判断用户是否已经加过购物车
        Cart cart = cartMapper.getCart(cartDTO.getProductId(), userLoginVO.getId());
        log.debug("获取的购物车:{}", cart);
        if (cart == null) {
            cart = new Cart();
            //从未被加入购物车，构建购物车
            cart.setUserId(userLoginVO.getId());
            cart.setProductId(product.getId());
            cart.setPrice(product.getPrice());
            cart.setNum(cartDTO.getNum());
            cart.setCreatedUser(userLoginVO.getUsername());
            cart.setModifiedUser(userLoginVO.getUsername());
            cartMapper.insertCart(cart);
        } else {
            cart.setNum(cart.getNum() + cartDTO.getNum());
            int result = cartMapper.updateNum(cart);
            // result
            log.debug("获取的购物车3:{}", cart);
            if (result != 1) {
                throw new ServiceException(ServiceCode.ERR_IS_ASSOCIATED, "更新商品数量失败");
            }
        }
    }

    @Override
    public List<CartVO> getCartlist(UserLoginVO userLoginVO) {

        System.out.println("id:"+userLoginVO.getId());
        List<CartVO> cartList = cartMapper.getCartlist(userLoginVO.getId());
        if(cartList == null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"没有商品分类信息");
        }
        return cartList;
    }

    /**
     * 购物车数量更改
     * @param cartNumUpdateDTO
     * @param userLoginVO
     */
    @Override
    public void updateCartNum(CartNumUpdateDTO cartNumUpdateDTO, UserLoginVO userLoginVO) {
        /**
         * 1.获取商品id、用户id
         * 2.更改数量，num+1.
         */
        //查询商品状态，是否被下架
        Product product = cartMapper.getProductId(cartNumUpdateDTO.getProductId());
        if (product == null || product.getStatus() != 1) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "商品不存在或已经下架");
        }
        //根据用户id和商品id返回对象
        Cart cart = cartMapper.getCart(cartNumUpdateDTO.getProductId(), userLoginVO.getId());
        log.debug("获取的购物车:{}", cart);
        //更新数量，+加一
        //          -减一
        if (cartNumUpdateDTO.getNumber()) {
            cart.setNum(cart.getNum() + 1);
        } else {
            cart.setNum(cart.getNum() - 1);
        }
        int result = cartMapper.updateNum(cart);
        System.out.println(result);
        if (result != 1) {
            throw new ServiceException(ServiceCode.ERR_IS_ASSOCIATED, "更新商品数量失败");
        }
    }

}
